package diagramobjects;
import java.awt.Graphics;
import java.util.ArrayList;

import utils.Color;
import utils.Point;



import program.PaintObjects;

/**
 * 
 * @author wojtek
 * obiekt Linia istniejacy na panelu
 */
public class ArrowsTextLine extends TextLine {
	

	protected Arrow BeginArrow;
	protected Arrow EndArrow;


	
	
	@Override
	/**
	 * uzywana do rysowania linii na panelu - wywolywana w Drawing Panel
	 */
	public void Draw(Graphics g, PaintObjects po) 
	{
		po.DrawArrowsLine(g, this);
	}
	
	@Override
	public void DrawSelection(Graphics g, PaintObjects po)
	{
		// TODO Auto-generated method stub
		po.DrawArrowsLineSelection(g, this);
	}

	
	
	public ArrowsTextLine(Integer id, Point begin, Point end,Integer dashing, Text caption) 
	{
		super(id, begin,end, dashing, caption);

		BeginArrow =new Arrow(Begin, End, false, new Color(255,0,0));
		EndArrow = new Arrow(End, Begin, false, new Color(255,0,0));

	}

	public ArrowsTextLine(Integer id, Point begin, Point end,Integer dashing, Text caption, Boolean beginArrow, Boolean endArrow) 
	{
		super(id, begin,end, dashing, caption);
		if (beginArrow == true)
			BeginArrow =new Arrow(Begin, End, false, new Color(255,0,0));
		else
			BeginArrow =new Arrow();
		if (endArrow == true)
			EndArrow = new Arrow(End, Begin, false, new Color(255,0,0));
		else
			EndArrow =new Arrow();

	}
	
	/**
	 * przesuwa punkt poczatkowy do okreslonej lokalizacji
	 * @param x
	 * @param y
	 */

	public void MoveBeginTo(Integer x, Integer y)
	{
		Begin.setX(x);
		Begin.setY(y);
		BeginArrow =new Arrow(Begin, End, false, new Color(255,0,0));
		EndArrow = new Arrow(End, Begin, false, new Color(255,0,0));
	}
	
	/**
	 * przesuwa punkt koncowy do okreslonej lokalizacji
	 * @param x
	 * @param y
	 */
	
	public void MoveEndTo(Integer nowex, Integer nowey)
	{
		End.setX(nowex);
		End.setY(nowey);
		BeginArrow =new Arrow(Begin, End, false, new Color(255,0,0));
		EndArrow = new Arrow(End, Begin, false, new Color(255,0,0));
		
		Double a =0.0;
		Double x = 0.0;
		Double y = 0.0;
		
		Double mianownikAlfa ;

			 mianownikAlfa = new Double(End.getX() - Begin.getX());
			if (mianownikAlfa == 0)
				mianownikAlfa = 0.000001;
			a = (End.getY() - Begin.getY())/mianownikAlfa;
			if (a == 0)
				a = 0.0000000001;
			System.out.println("Aif: " +  a.toString());
	
		Double alfa = java.lang.Math.atan(a);
		alfa = alfa - java.lang.Math.PI/2;
		
		if (mianownikAlfa >= 0)
		{
			alfa = alfa + java.lang.Math.PI;
		}
		
		
		Double tmpDodatkowyKat = java.lang.Math.PI;
		if (alfa < 0 )
			tmpDodatkowyKat = 0.0;
		
		System.out.println("AAAALFAAAA: " + alfa);
		Caption.setPosition(Caption.getPosition().Move(new Point(End.getX() - (End.getX() - Begin.getX())/2, End.getY() - (End.getY() - Begin.getY())/2), 10, alfa + tmpDodatkowyKat));
		Caption.Rotate(alfa + tmpDodatkowyKat);
	}
	
	public void MoveEndTo(Integer nowex, Integer nowey, Boolean beginArrow, Boolean endArrow)
	{
		End.setX(nowex);
		End.setY(nowey);
		if (beginArrow == true)
			BeginArrow =new Arrow(Begin, End, false, new Color(255,0,0));
		else
			BeginArrow =new Arrow();
		if (endArrow == true)
			EndArrow = new Arrow(End, Begin, false, new Color(255,0,0));
		else
			EndArrow =new Arrow();

		
		Double a =0.0;
		Double x = 0.0;
		Double y = 0.0;
		
		Double mianownikAlfa ;

			 mianownikAlfa = new Double(End.getX() - Begin.getX());
			if (mianownikAlfa == 0)
				mianownikAlfa = 0.000001;
			a = (End.getY() - Begin.getY())/mianownikAlfa;
			if (a == 0)
				a = 0.0000000001;
			System.out.println("Aif: " +  a.toString());
	
		Double alfa = java.lang.Math.atan(a);
		alfa = alfa - java.lang.Math.PI/2;
		
		if (mianownikAlfa >= 0)
		{
			alfa = alfa + java.lang.Math.PI;
		}
		
		
		Double tmpDodatkowyKat = java.lang.Math.PI;
		if (alfa < 0 )
			tmpDodatkowyKat = 0.0;
		
		System.out.println("AAAALFAAAA: " + alfa);
		Caption.setPosition(Caption.getPosition().Move(new Point(End.getX() - (End.getX() - Begin.getX())/2, End.getY() - (End.getY() - Begin.getY())/2), 10, alfa + tmpDodatkowyKat));
		Caption.Rotate(alfa + tmpDodatkowyKat);
	}

	/**
	 * sluzy zapisaniu danych do bazy danych - zwraca fragment potrzebnego zapytania
	 * @return
	 */
	public String ToDatabase()
	{
		
		String toReturn = /* Id.toString() + "," +*/
		Begin.getX().toString() + "," +
		Begin.getY().toString() + "," +
		End.getX().toString() + "," +
		End.getY().toString();
		
		return toReturn;
		
	}
	
	/**
	 * drukuje istniejaca linie - DO TESTOW
	 */
	
	public void Print()
	{
		String toReturn = Id.toString() + "," +
		Begin.getX().toString() + "," +
		Begin.getY().toString() + "," +
		End.getX().toString() + "," +
		End.getY().toString();

		System.out.println("PRINTING LINE " + toReturn);
	}
	
	public Boolean PointCollision(Integer x, Integer y)
	{
		Double EndXMBeginX = new Integer(End.getX() - Begin.getX()).doubleValue();
		if (EndXMBeginX  == 0)
			EndXMBeginX = 1.0;
		Double odlOdProstej = -((End.getY()-Begin.getY())/(EndXMBeginX))*x + 1*y - (End.getY() - ((End.getY()-Begin.getY())/(EndXMBeginX))*End.getX());
		System.out.println("ODL" + odlOdProstej);
		
		
		if (odlOdProstej < 5)
			return true;
		else
			return false;
		
	}
	
	

	/**
	 * @return the beginArrow
	 */
	public Arrow getBeginArrow()
	{
		return BeginArrow;
	}

	/**
	 * @return the endArrow
	 */
	public Arrow getEndArrow()
	{
		return EndArrow;
	}

	public Integer getDashing()
	{
		return Dashing;
	}

	@Override
	public void MoveByVector(Integer vx, Integer vy)
	{
		// TODO Auto-generated method stub
		super.MoveByVector(vx, vy);
		BeginArrow.MoveByVector(vx, vy);
		EndArrow.MoveByVector(vx, vy);
	}
	

	
}
